import {
	getFuzhi,
	getshanchu
} from '@/api/myapi/jiluwenjian';
import {
	fileSuffix
} from '@/util/common-data.js';
import {
	getAlluser,
	generateImage
} from '@/api/myapi/common';
export default {
	data() {
		return {
			templeteIndex: 0,
			upLoadIndex: 0,
			attachBox: false,
			src: '',
			Alluser: [],
			attachForm: {},
			attachOption: {
				submitBtn: false,
				emptyBtn: false,
				column: [{
					label: '附件上传',
					prop: 'attachFile',
					type: 'upload',
					showFileList: false,
					drag: true,
					loadText: '文件上传中，请稍等',
					span: 24,
					propsHttp: {
						res: 'data',
						url: 'link',
						home: 'domain',
						name: 'originalName'
					},
					action: '/api/blade-resource/oss/endpoint/put-file-attach'
				}]
			}
		}
	},
	methods: {
		uploadPreview(file, column, done) {
			console.log(file)
			this.preContent(file, 6)
		},
		isSystemCoverChange(checked,form,type){
			if(!type){
				if(!form.change){
					this.$message.error('请删除旧版文件!');
					return
				}
			}
			checked?form.isSystemCover=1:form.isSystemCover=0;
		},
		//  获取封面
		getFileCover(form, url) {
			console.log(form)
			let arr = [
				{
					value: form.name,
					tips: '请补充文件名称'
				},
				{
					value: form.fileNumber,
					tips: '请输入文件编号'
				},
				{
					value: form.versionNumber,
					tips: '请输入文件版本号'
				},
				{
					value: form.pageCount,
					tips: '请输入页码数量'
				}
			];
			for (let j = 0; j < arr.length; j++) {
				if (!arr[j].value) {
					this.$message.error(arr[j].tips);
					return;
				}
			}
			let fileItem = {};
			let FilesProcessRecordList = this.$refs.creatFlow ? this.$refs.creatFlow.guifanFilesProcessRecordList : this
				.$parent.$parent.$refs.creatFlow.guifanFilesProcessRecordList;
			let isOnlineReview = this.$refs.creatFlow ? this.$refs.creatFlow.isOnlineReview : this.$parent.$parent.$refs
				.creatFlow.isOnlineReview;
			fileItem = Object.assign(fileItem, form);
			fileItem.depts = "";
			fileItem.createDeptName = this.userInfo.deptName;
			fileItem.createUserName = this.userInfo.user_name;
			fileItem.createUserId = this.userInfo.user_id;
			fileItem.createDeptId = this.userInfo.dept_id;
			fileItem.isOnlineReview = isOnlineReview ? 1 : 0;
			fileItem.versionNumber=form.nextVersionNumber?form.nextVersionNumber:fileItem.versionNumber;
			console.log(JSON.stringify(fileItem));
			if (url == 'systemjilufilesprocess') {
				fileItem.jiluFilesProcessRecordList = FilesProcessRecordList;
			} else if (url == 'systemguifanfilesprocess') {
				fileItem.guifanFilesProcessRecordList = FilesProcessRecordList;
			} else if (url == 'systemchengxufilesprocess') {
				fileItem.chengxuFilesProcessRecordList = FilesProcessRecordList;
			} else if (url == 'systemwailaifilesprocess') {
				fileItem.wailaiFilesProcessRecordList = FilesProcessRecordList;
			} else {
				fileItem.zhiliangFilesProcessRecordList = FilesProcessRecordList;
			}
			console.log(fileItem)
			generateImage(fileItem, url).then(({
				data
			}) => {
				if (data.success) {
					console.log(data)
					if (data.data) {
						this.src = data.data;
						this.$refs.pdf.open();
					}
				}
			})
		},
		upload(form, index) {
			this.upLoadIndex = index;
			this.attachForm = {};
			this.attachBox = true;
		},
		uploadAfterdetails(res, done, loading, column) {
			console.log(res)
			this.detail.templateFile = res.link;
			this.detail.fileId = res.attachId;
			this.detail.isSystemTemplate = 0;
			this.detail.originalName = res.originalName;
			this.attachBox = false;
			done();
		},
		uploadAfter(res, done, loading, column) {
			this.formList[this.upLoadIndex].templateFile = res.link;
			this.formList[this.upLoadIndex].templateFilex = res.link;
			this.formList[this.upLoadIndex].fileId = res.attachId;
			this.formList[this.upLoadIndex].isSystemTemplate = 0;
			this.formList[this.upLoadIndex].originalName = res.originalName;
			if (!this.formList[this.upLoadIndex].name) {
				this.$set(this.formList[this.upLoadIndex],'name',res.originalName.split('.')[0])
			}
			this.attachBox = false;
			done();
		},
		delFileTemplete(form, type) {
			if(type)  form.change=true;
			if (form.isSystemTemplate == 1) {
				getshanchu({
					templateFile: form.templateFilex
				}).then(({
					data
				}) => {
					if (data.code == 200) {
						form.templateFile="";
						form.isSystemTemplate = 0;
						form.isSystemCover = 0;
						if (!type) form.name = "";
						if (this.$refs.form) this.$refs.form.resetFields();
					}
				});
			} else {
				form.templateFile="";
				form.isSystemTemplate = 0;
				form.isSystemCover = 0;
				if (!type) form.name = "";
				if (this.$refs.form) this.$refs.form.resetFields();
			}
			this.$forceUpdate();
		},
		fileTempleteEdit(form) {
			if (form.templateFilex) {
				this.preContent(form, 2);
			} else {
				this.preContent(form, 5);
			}

		},
		templeteBtnClick(index) {
			this.templeteIndex = index;
			this.openDialg('template');
		},
		uploadBefore(file, done, loading, column) {
			const fileName = file.name;
			const fileStrArr = fileName.split('.');
			const suffix = fileStrArr[fileStrArr.length - 1];
			let result = fileSuffix.some(item => {
				return item === suffix;
			});
			if (!result) {
				loading();
				this.$message.error('不支持该文件类型');
				return false;
			}
			done();
		},
		// 部门列表
		getAlluser() {
			getAlluser({
				current: 1,
				size: 100000
			}).then(({
				data
			}) => {
				if (data && data.success) {
					data.data.records.forEach(item => {
						this.Alluser.push({
							label: `${item.deptName}/${item.name}`,
							value: item.id,
							deptId: item.deptId
						});
					});
				}
			});
		},
		async templateConfirm(templateFile) {
			templateFile.templateFile = templateFile.files;
			let index = this.templeteIndex;
			this.formList[index].isSystemTemplate = 1;
			this.formList[index].templateFile = templateFile.files;
			this.formList[index].originalName = templateFile.name;
			if (!this.formList[index].name) this.formList[index].name = templateFile.name;
			this.fuizhiFile([templateFile], this.formList[index]);
			this.$forceUpdate();
		},
		fuizhiFile(fileList, form) {
			getFuzhi({
				templateFileList: fileList
			}).then(({
				data
			}) => {
				if (data.code == 200 && data.data.length != 0) {
					form.templateFilex = data.data[0].url;
				}
			})
		}
	}
}
